﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MS.InformationManagement.Presenter.InsertarRadicado;
using System.Collections.Generic;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.Presenter.TRDConfig;
using MS.InformationManagement.Support;
using MS.InformationManagement.Presenter.TRDConfig.Events;
using System.Linq;
using System.Text;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Presenter.Support.Events;
using System.Collections;
using MS.InformationManagement.DAL.RecordCenter;

namespace MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls
{
    public partial class InsertarRadicado : UserControl, IInsertarRadicadoView
    {
        #region fields

        private InsertarRadicadoPresenter _presenter;

        #endregion

        #region Properties

        public int RemitenteID { get; set; }
        public int seccionID { get; set; }
        public int serieID { get; set; }

        public List<ListItem> ListaTipos { get; set; }
        public List<ListItem> ListaModoRecepcion { get; set; }
        public Remitentes remitente { get; set; }
        static public ArrayList arrayFiles = new ArrayList();
        public IEnumerable<Seccion> Seccion { get; set; }
        public IEnumerable<SubseccionesSubseccion> Subseccion { get; set; }
        public IEnumerable<Serie> Series { get; set; }
        public IEnumerable<SubseriesSubserie> Subseries { get; set; }
        #endregion

        #region Form Events

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            labelControls(true);
        }
             
        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new InsertarRadicadoPresenter(this);
            //if (IsPostBack) return;

            _presenter.HandleLoad(Request.QueryString);

            if (remitente != null)
            {
                txtNITCedula.Text = remitente.NITCedulaRemitente;
                txtForm1.Text = remitente.NombresRemitente;
                txtForm2.Text = remitente.ApellidosRemitente;
                ddlTipo.SelectedItem.Value = remitente.TipoRemitente.ToString();
                btnAdd.Enabled = true;
                setControls(false);
                
                //txtTelefono.Required("Este campo es de caracter obligado", AjaxControlToolkit.ValidatorCalloutPosition.Right);
                //txtDireccion.Required("Este campo es de caracter obligado", AjaxControlToolkit.ValidatorCalloutPosition.Right);
                //txtEmail.Required("Este campo es de caracter obligado", AjaxControlToolkit.ValidatorCalloutPosition.Right);
                //txtAsunto.Required("Este campo es de caracter obligado", AjaxControlToolkit.ValidatorCalloutPosition.Right);
            }
        }

        protected void ImbBtnSearch_Click(object sender, ImageClickEventArgs e)
        {
            _presenter.LoadPopUp(this.Page);
        }

        protected void ddlTipo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlTipo.SelectedItem.Text == CustomSiteColumns.FIELD_REMITENTE_CIUDADANO)
            {
                labelControls(true);
            }
            else
            {
                labelControls(false);
            }
        }

        protected void btnAddAdjunto_Click(object sender, System.EventArgs e)
        {
            if (Page.IsPostBack == true)
            {
                if (FileUploadAdjuntos.PostedFile != null && FileUploadAdjuntos.HasFile)
                {
                    arrayFiles.Add(FileUploadAdjuntos);
                    ListBoxAdjuntos.Items.Add(FileUploadAdjuntos.PostedFile.FileName);
                }
            }
        }

        protected void btnDeleteAdjunto_Click(object sender, System.EventArgs e)
        {
            if (ListBoxAdjuntos.Items.Count != 0)
            {
                arrayFiles.RemoveAt(ListBoxAdjuntos.SelectedIndex);
                ListBoxAdjuntos.Items.Remove(ListBoxAdjuntos.SelectedItem.Text);
            }
        }

        public new event Presenter.Support.Events.ErrorHandler OnError;

        public event Presenter.Support.Events.SuccessHandler OnSuccess;

        #endregion

        #region Implemented Methods

        public void Show()
        {
            ddlTipo.DataSource = ListaTipos;
            ddlTipo.DataBind();

            ddlModoRecepcion.DataSource = ListaModoRecepcion;
            ddlModoRecepcion.DataBind();
            
        }

        public void ShowPopUp()
        {
            
        }

        void labelControls(bool esCiudadano)
        {
            if (esCiudadano == true)
            {
                lblID.Text = "Cedula";
                lblForm1.Text = "Nombre";
                lblForm2.Text = "Primer apellido";
                lblForm3.Text = "Segundo apellido";
            }
            else
            {
                lblID.Text = "NIT";
                lblForm1.Text = "Razón Social";
                lblForm2.Text = "Sigla";
                lblForm3.Text = "Representante Legal";
            }
        }

        void setControls(bool state)
        {
            ddlTipo.Enabled = state;
            txtForm1.Enabled = state;
            txtNITCedula.Enabled = state;
            txtForm2.Enabled = state;
            txtForm3.Enabled = state;
            ImbBtnSearch.Enabled = state;
        }

        #endregion

       
    }
}
